<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/3/8
 * Time: 10:03
 */

namespace app\common\model;


use think\Cache;

/**教学视频评论回复相关模型
 * Class ZhunleCourseCommentReply
 * @package app\common\model
 */
class ZhunleCourseCommentReply extends Base
{

    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_course_comment_reply';

    const COURSE_COMMENT_REPLY_IDS = 'course:comment:reply:ids:';//当前评论下的所有回复id
    const COURSE_COMMENT_REPLY_INFO = 'course:comment:reply:info:';//当前回复的详情
    const CACHE_TIME = 86400 * 7;

    /**获取当前评论的回复
     * @param array $map
     * @return array
     */
    public function getCommentReply($map = [])
    {
        $id = $map['id'];

        $data_list = [];

        $reply_ids = false;

        if ($this->cache_on >= 1)
        {
            $reply_ids = Cache::get(self::COURSE_COMMENT_REPLY_IDS . $id);
        }

        if ($reply_ids === false)
        {
            $where = [
                'status' => 0,
                'cid' => $id
            ];

            try {
                $data = $this
                    ->where($where)
                    ->column('id');
                if (!empty($data)) {
                    $reply_ids = implode(',', $data);
                } else {
                    $reply_ids = '';
                }

                if ($reply_ids && $this->cache_on >= 1) {
                    Cache::set(self::COURSE_COMMENT_REPLY_IDS . $id, $reply_ids, self::CACHE_TIME);
                }

            } catch (\Exception $e) {
                $this->error = '获取当前评论的回复信息失败.';
                return false;
            }
        }

        // 将课程评论id转为数组
        $reply_ids = explode(',', $reply_ids);
        if (!empty($reply_ids))
        {
            $ac_ids = '';
            $temp = [];
            foreach ($reply_ids as $ac_id)
            {
                if ($this->cache_on >= 1)
                {
                    //获取当前回复的详情
                    $temp[$ac_id] = Cache::get(self::COURSE_COMMENT_REPLY_INFO . $ac_id);
                } else
                    {
                    $temp[$ac_id] = false;
                }

                //若当前回复未缓存,获取所有未缓存的回复id,用以从数据库中获取
                if ($temp[$ac_id] === false && $ac_id)
                {
                    $ac_ids .= $ac_id . ',';
                }
            }

            if ($ac_ids != '' || strlen($ac_ids) > 0)
            {
                //去掉结尾的 ，
                $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);
                try {

                    $reply = $this
                        ->field('id,content')
                        ->where('status', 0)
                        ->where('id in(' . $ac_ids . ')')
                        ->order('create_time', 'DESC')
                        ->select();
                    $reply = json_decode(json_encode($reply, true), true);

                } catch (\Exception $e) {
                    $this->error = '获取回复信息失败';
                    return false;
                }

                if (!empty($reply)) {
                    foreach ($reply as $item) {
                        if ($this->cache_on >= 1) {
                            Cache::set(self::COURSE_COMMENT_REPLY_INFO . $item['id'], $item, self::CACHE_TIME);
                        }

                        if ($item) {
                            $temp[$item['id']] = $item;
                        }
                    }
                }
            }

            foreach ($temp as $k => $v)
            {
                if ($v)
                {
                    $data_list[$k]['name'] = '小准准';
                    $data_list[$k]['content'] = $v['content'];
                }
            }

            $data_list = array_values($data_list);

        }

        return $data_list;
    }
}